package com.wandian.setting.mvvm.setting;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.AppOpenBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.dialog.CommonDialogTextChoose;
import com.mylib.libcore.dialog.VersionUpDialog;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.tools.TTAdManagerHolder;
import com.mylib.libcore.utils.AppUtil;
import com.mylib.libcore.utils.DataCleanManager;
import com.mylib.libcore.utils.StatusBarUtils;
import com.mylib.libcore.utils.TabKeyUtils;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.setting.R;
import com.wandian.setting.databinding.SetActivitySettingBinding;

import static com.mylib.libcore.config.AppConfig.CSJ_OPEN_ID;
import static com.mylib.libcore.config.AppSPKeyConfig.APP_FUNCTION_TABS;
import static com.mylib.libcore.config.AppSPKeyConfig.IS_FIRST_OPEN;
import static com.mylib.libcore.config.AppSPKeyConfig.IS_OPEN_AD;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 设置
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_SETTING)
public class SettingActivity extends  BaseMVVMActivity<SettingActivityViewModel> implements CommonDialogTextChoose.CommonDialogTextBtnClink {
    private SetActivitySettingBinding dataBinding;
    private int app_number = 1;//当期版本号
    private VersionUpDialog versionUpDialog;//升级弹框
    private CommonDialogTextChoose commonDialogTextChoose;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.set_activity_setting);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initClink();

    }
    private void initViewData(){
        StatusBarUtils.with(this,false)
                .setIsActionBar(false)
                .clearActionBarShadow()
                .setDrawable(getResources().getDrawable(R.drawable.sys_color))
                .init();
        dataBinding.bindSysToolBar.toolBarTvTitle.setText("设置");
        app_number= AppUtil.getPackageVersionCode(this);
        if(UIUtils.isEmpty(app_number)){
            app_number =1;
        }
        mViewModel.appOpenBean.observe(this,appOpenBean -> {
            if(!UIUtils.isEmpty(appOpenBean)){
                if(appOpenBean.getAuditV()>app_number)showVersionDialog(appOpenBean);
                else ToastUtil.show("当前已是最新版本");
            }
        });
    }

    private void initClink(){
        dataBinding.bindSysToolBar.toolBarIvBack.setOnClickListener(v -> {
            finishhideSoftBoard();
            finish();
        });
        //个人资料
        dataBinding.bindNextPageText1.tvLeft.setText("个人资料");
        dataBinding.bindNextPageText1.rlNextPage.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_SETTING_USER_SETTING).navigation());
        //账号安全
        dataBinding.bindNextPageText2.tvLeft.setText("账号与安全");
        dataBinding.bindNextPageText2.rlNextPage.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_SETTING_ACCOUNT_SECURITY).navigation());
        //账号安全
        dataBinding.bindNextPageText2.tvLeft.setText("账号与安全");
        dataBinding.bindNextPageText2.rlNextPage.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_SETTING_ACCOUNT_SECURITY).navigation());


        //消息通知
        dataBinding.bindNextPageText5.tvLeft.setText("消息通知");
        dataBinding.bindNextPageText5.rlNextPage.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_SETTING_MESSAGE_SET).navigation());
        //清除缓存
        try {
            dataBinding.bindNextPageText6.tvRight.setText(DataCleanManager.getTotalCacheSize(SettingActivity.this)+"");
        } catch (Exception e) {
            e.printStackTrace();
            dataBinding.bindNextPageText6.tvRight.setText("0M");
        }
        dataBinding.bindNextPageText6.tvLeft.setText("清除缓存");
        dataBinding.bindNextPageText6.rlNextPage.setOnClickListener(v -> {
            DataCleanManager.clearAllCache(SettingActivity.this);
            dataBinding.bindNextPageText6.tvRight.setText("0M");
            ToastUtil.show("已清除");
        });
        //关于APP
        dataBinding.bindNextPageText7.tvLeft.setText("关于APP");
        dataBinding.bindNextPageText7.rlNextPage.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_SETTING_ABOUT).navigation());
        //检测版本更新
        dataBinding.bindNextPageText8.tvLeft.setText("检测版本更新");
        dataBinding.bindNextPageText8.rlNextPage.setOnClickListener(v -> mViewModel.getVersion(app_number));
        //注销账号
        dataBinding.tvZx.setOnClickListener(v ->showDeleteDialog("是否注销当前账号，系统将清空用户数据。","","") );
    }


    //展示失败弹框
    private void showDeleteDialog(String title,String type,String id){
        try {
            commonDialogTextChoose = new CommonDialogTextChoose(this,title,type,id,this::sureClink);
            //显示窗口
            //设置layout在PopupWindow中显示的位置
            if(!UIUtils.isEmpty(dataBinding.llSetting)){
                commonDialogTextChoose.showAtLocation(dataBinding.llSetting, Gravity.CENTER, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //展示弹框
    private void showVersionDialog(AppOpenBean appOpenBean){
        try {
            versionUpDialog = new VersionUpDialog(getContext(),appOpenBean,null);
            //显示窗口
            //设置layout在PopupWindow中显示的位置
            if(!UIUtils.isEmpty(dataBinding.llSetting)){
                versionUpDialog.showAtLocation(dataBinding.llSetting, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sureClink(String text, String type, String id) {
        ToastUtil.show("已注销");
        AppContext.getInstance().getAppPreferencesHelper().clearPreferencesHelper();
        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_LOGIN).navigation();
    }
}
